package com.zrf.action;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.zrf.service.OrderService;
import com.zrf.service.ProductService;
import com.zrf.service.SellerService;
import com.zrf.service.ShoppingCartService;


@RequestMapping("/sca")
@Controller
public class ShoppingCartAction {

	
	@Autowired
	private ProductService pservice ;
	@Autowired
	private ShoppingCartService scservice;
	@Autowired
	private OrderService orders;
	@Autowired
	private SellerService ms;
	
	
	
	
	@RequestMapping(value="addShoppingCart",
			params={"productid","num","bname","bpassword"})
	public String addShoppingCart(@RequestParam("productid") Integer productid,
			@RequestParam("num") int num,@RequestParam("bname") String bname,
			@RequestParam("bpassword") String bpassword){
		
		try {
			if(num==0){
				return "redirect:/la/buyerlogin?&name="+bname+"&password="+bpassword;
			}
			if(pservice.addShoppingCart(bname, productid, num)){
				return "redirect:/la/buyerlogin?&name="+bname+"&password="+bpassword;
			}
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		}
		
		
		return "fail";
	}
	
	
	
	
	
	@RequestMapping(value="deleteShoppingCart",params={"scartid","bname","bpassword"})
	public String deleteShoppingCart(@RequestParam("scartid") Integer scartid,
			@RequestParam("bname") String bname,@RequestParam("bpassword") String bpassword){
		
		try {
			if(scservice.deleteShoppingcart(scartid)){
				return "redirect:/la/buyerlogin?&name="+bname+"&password="+bpassword;
			}
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		}
		
		
		return "fail";
	}
	
	
	
	@RequestMapping(value="addOrder", params={"uname","pwd"})
	public String addOrder(@RequestParam("uname") String uname,
						   @RequestParam("pwd") String pwd){
		try {
			if (orders.addOrder(uname)) {
				return "redirect:/spa/ulogin?&name="+uname+"&pwd="+pwd;
			}
		} catch (Exception e) {
			// TODO: handle exception

			e.printStackTrace();
		}
		return "fail";
	}
	
	@RequestMapping(value="goOrder", params={"uname","pwd"})
	public String goOrder(@RequestParam("uname") String uname,
						   @RequestParam("pwd") String pwd,
						   Map<String, Object> map){
		//0是未付款，1是未收货，2是完成订单，3是已取消订单

		map.put("tMList", orders.findByType(uname, 0));
		map.put("tHList", orders.findByType(uname, 1));
		map.put("fList", orders.findByType(uname, 2));
		map.put("dList", orders.findByType(uname, 3));
		
		map.put("pwd", pwd);
		map.put("uname", uname);
		return "Oder";
	}
}
